package com.tql.direct;

import com.rabbitmq.client.*;
import com.tql.util.ConnectionUtil;

import java.io.IOException;

public class Consumer {
        private static Runnable runnable=new Runnable() {
            public void run() {
                   String QUEUE_NAME=Thread.currentThread().getName();
                    Connection connection=null;
                    try{
                        //获取连接对象
                        connection= ConnectionUtil.getConnection();
                        //创建频道
                        Channel channel = connection.createChannel();

                        /*    * @param1 队列名称
                         * @param2 是否持久化
                         * @param3 是否支持排他性
                         * @param4 最后一个消费者消费完消息是否自动删除，
                         * @parma5 传入参数
                         * */
                        //获取频道参数
                        System.out.println("________________");
                        channel.basicConsume(QUEUE_NAME, true, new DeliverCallback() {
                            public void handle(String s, Delivery delivery) throws IOException {
                                System.out.println("接收到的消息为"+new String(delivery.getBody(),"utf-8"));
                                System.out.println("+++++++++++++++++++++++++++");
                            }
                        }, new CancelCallback() {
                            public void handle(String s) throws IOException {
                                System.out.println("接收信息失败");
                            }
                        });
                        System.out.println(QUEUE_NAME+"开始接收消息");
                        System.in.read();
                    }catch (Exception e){
                        e.printStackTrace();
                    }
            }
        };
    public static void main(String[] args) {
        new Thread(runnable,"queue1").start();
        new Thread(runnable,"queue2").start();
        new Thread(runnable,"queue3").start();
    }
}
